package com.wthink.newMain.settings.config.inner.userConfig

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.data.Result
import com.wthink.hzgj.data.domain.User
import com.wthink.hzgj.service.DevService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class UserConfigViewmodel : ViewModel(){

    val userList = MutableLiveData(emptyList<User>())
    val user1 = MutableLiveData<User>()
    val user2 = MutableLiveData<User>()

    fun updatePwd(index : Int ){}

    fun getUsers() {
        viewModelScope.launch (Dispatchers.IO){
            val ret = DevService.getUser()
            if (ret is Result.Success) {
                val list = ret.data.list
                userList.postValue(list)
                user1.postValue(list[1])
                user2.postValue(list[2])
            }
        }
    }

    fun setUser(index: Int, admin: String, pwd: String) {
        viewModelScope.launch(Dispatchers.IO){
            val ret = DevService.setUser(User(index, admin), pwd, "mod")
            if (ret is Result.Success) {
                getUsers()
            }
        }
    }
}